package com.ruoyi.product.domain;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import com.ruoyi.common.annotation.Excel;
import com.ruoyi.common.core.domain.BaseEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * 商品规格属性对象 product_spec
 * 
 * @author liuhuizhong
 * @date 2025-08-12
 */
@Data
public class ProductSpec extends BaseEntity
{
    private static final long serialVersionUID = 1L;

    /** 编号 */
    @Excel(name = "编号")
    private Long id;

    /** 属性名称（如"Color"） */
    @Excel(name = "属性名称", readConverterExp = "如Color")
    private String name;

    private Long templateId;

    private Integer type;

    /** 可选值 */
    @Excel(name = "可选值")
    private String valuesJson;

    /** 影响SKU生成 */
    @Excel(name = "影响SKU生成")
    private Long isSku;

    /** 允许自定义输入 */
    @Excel(name = "允许自定义输入")
    private Long isCustom;

    /** 单位 */
    @Excel(name = "单位")
    private String unit;

    /** 排序权重 */
    @Excel(name = "排序权重")
    private Long sort;

    @Override
    public String toString() {
        return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
            .append("id", getId())
            .append("name", getName())
            .append("valuesJson", getValuesJson())
            .append("isSku", getIsSku())
            .append("isCustom", getIsCustom())
            .append("unit", getUnit())
            .append("sort", getSort())
            .append("createBy", getCreateBy())
            .append("updateBy", getUpdateBy())
            .append("createTime", getCreateTime())
            .append("updateTime", getUpdateTime())
            .toString();
    }

    @JsonIgnore
    public String getValuesJson() {
        return valuesJson;
    }

    @JsonProperty("valuesJson")
    public List<String> getValuesJsonAsList() {
        if (valuesJson != null && !valuesJson.isEmpty()) {
            try {
                ObjectMapper objectMapper = new ObjectMapper();
                return objectMapper.readValue(valuesJson, new TypeReference<List<String>>(){});
            } catch (Exception e) {
                return new ArrayList<>();
            }
        }
        return new ArrayList<>();
    }
}
